package com.example.backend.controller;

import com.example.backend.pojo.User;
import com.example.backend.response.LoginResponse;
import com.example.backend.response.VisitedResponse;
import com.example.backend.service.LogsService;
import com.example.backend.service.UserService;
import com.example.backend.util.Response;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LogsService logsService;

    @Data
    private static class LoginRequest {
        private String username;
        private String password;
    }

    @Data
    private static class GetUserInfoRequest {
        private int id;
    }

    @PostMapping("/login")
    public Response<LoginResponse> login(@RequestBody LoginRequest user) {
        return userService.login(user.getUsername(), user.getPassword());
    }

    @PostMapping("/register")
    public Response<User> create(@RequestBody User user) {
        return userService.create(user);
    }

    @PostMapping("/getUserInfo")
    public Response<User> findUserById(@RequestBody GetUserInfoRequest user) {
        return userService.findUserById(user.getId());
    }

    @PostMapping("/updateInfo")
    public Response<Object> updateInfo(@RequestBody User user) {
        return userService.update(user);
    }

    @PostMapping("/exist")
    public Response<Object> exist(@RequestBody LoginRequest user) {
        return userService.exist(user.getUsername());
    }

    @PostMapping("/model")
    public Response<Object> model(@RequestBody User user) {
        return userService.updateModel(user);
    }

    @GetMapping("/visited-rooms")
    public Response<List<VisitedResponse>> findAllVisited(@Param("username") User user) {
        return logsService.findAllVisited(user.getUsername());
    }
}
